package com.hxh.entity.po;

import cn.hutool.core.util.StrUtil;
import com.hxh.entity.ProjectTableFiledConfig;
import lombok.Data;

import java.sql.ResultSet;
import java.sql.SQLException;

@Data
public class FieldName implements ExplainByResultSet<FieldName>{
    private String name;
    private String type;
    private String desc;

    private String key;
    private String defaultValue;

    //auto_increment
    private String extra;
    private boolean autIncrement;
    private boolean notNull;

    private String javaName;
    private String javaType;
    private int maxLength;

    private ProjectTableFiledConfig ProjectTableFiledConfig;

    public String jdbcTypeToJavaType(String sqlType) {
        if (sqlType == null || sqlType.trim().isEmpty()) {
            return sqlType;
        }
        if (sqlType.contains("(")){
            String maxLengthStr = sqlType.substring(sqlType.indexOf("(")+1,sqlType.indexOf(")"));
            try{
                this.maxLength = Integer.parseInt(maxLengthStr);
            }catch (Exception ignored){}
            sqlType = sqlType.substring(0,sqlType.indexOf("("));
        }

        sqlType = sqlType.toLowerCase();
        switch (sqlType) {
            case "nvarchar":
            case "char":
            case "json":
            case "varchar":
            case "text":
            case "mediumtext":
            case "nchar":
                //Java 的 long 是 64 位整数，取值范围为 -2^63 ~ 2^63 - 1。
                //JavaScript 中的数字类型是 Number，底层使用 IEEE 754 双精度浮点数表示，能准确表示的整数范围为 -2^53 ~ 2^53（即 ±9007199254740991）。
                //会丢失精度 直接使用String，让数据库转类型
            case "bigint":
            case "bigint unsigned":
                return "String";
            case "blob":
                return "byte[]";
            case "integer":
            case "int":
            case "int unsigned":
            case "tinyint":
            case "tinyint unsigned":
            case "smallint":
            case "mediumint":
                return "Integer";
            case "bit":
            case "boolean":
                return "Boolean";
            case "id":
                return "Long";
            case "float":
                return "Float";
            case "double":
                return "Double";
            case "decimal":
                return "java.math.BigDecimal";
            case "date":
            case "datetime":
            case "year":
            case "time":
            case "timestamp":
                return "java.util.Date";
            case "numeric":
            case "real":
                return "java.math.BigDecimal";
            case "money":
            case "smallmoney":
                return "Double";
            case "image":
                return "byte[]";
            default:
                throw new RuntimeException("-----------------》转化失败：未发现的类型" + sqlType);
        }
    }

    @Override
    public FieldName getOneByRs(ResultSet resultSet) throws SQLException {
        FieldName tableInfo = this;
        tableInfo.setKey(resultSet.getString("Key"));

        tableInfo.setName(resultSet.getString("Field"));
        tableInfo.setDefaultValue(resultSet.getString("Default"));
        tableInfo.setType(resultSet.getString("Type"));
        tableInfo.setExtra(resultSet.getString("Extra"));
        tableInfo.setAutIncrement("auto_increment".equalsIgnoreCase(resultSet.getString("Extra")));
        tableInfo.setNotNull("NO".equalsIgnoreCase(resultSet.getString("Null")));
        tableInfo.setJavaType(jdbcTypeToJavaType(resultSet.getString("Type")));
        tableInfo.setJavaName(StrUtil.toCamelCase(resultSet.getString("Field")));
        tableInfo.setDesc(resultSet.getString("Comment"));
        return tableInfo;
    }

}
